package com.bulletelc.apollopre.module.main.center.intention.list.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.entity.IntentionBean;
import com.bulletelc.apollopre.framework.MVPBaseFragment;
import com.bulletelc.apollopre.module.main.center.quotation.QuotationListActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/16.
 * 意向单Fragment
 */

public class IntentionListFragment extends MVPBaseFragment<IntentionListContract.View, IntentionListPresenter> implements IntentionListContract.View,
        AdapterView.OnItemClickListener {

    private ListView lvMain;
    private List<IntentionBean> intentionBeanList;
    private IntentionListAdapter adapter;
    private boolean first = true;

    @Override
    public int getLayoutRes() {
        return R.layout.intention_list_fragment;
    }

    @Override
    public void assignViews() {
        lvMain = findView(R.id.lvMain);
    }

    @Override
    public void setListener() {
        lvMain.setOnItemClickListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        intentionBeanList = new ArrayList<>();
        adapter = new IntentionListAdapter(baseActivity, intentionBeanList);
        lvMain.setAdapter(adapter);
    }

    @Override
    public void onSuccessIntentList(List<IntentionBean> intentionBeanList) {

    }

    @Override
    public void onErrorIntentList(String error) {

    }

    @Override
    public void onFailIntentList(String fail) {

    }

    @Override
    protected IntentionListPresenter createPresenter() {
        return new IntentionListPresenter();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(baseActivity, QuotationListActivity.class);
        intent.putExtra(IntentCode.EXTRA_INTENTION_ORDER_ID, intentionBeanList.get(i).getId());
        baseActivity.startActivity(intent);
    }

    /**
     * 意向报价排序
     * @param list
     */
    public void sortQuotationState(List<IntentionBean> list) {
        intentionBeanList.clear();
        for (int i = 0; i < list.size(); i++) {
            int state = list.get(i).getState();
            if (state == 15) {
                intentionBeanList.add(list.get(i));
            }
        }
        adapter.notifyDataSetChanged();
    }

    /**
     * 意向完成排序
     * @param list
     */
    public void sortCompleteState(List<IntentionBean> list) {
        intentionBeanList.clear();
        for (int i = 0; i < list.size(); i++) {
            int state = list.get(i).getState();
            if (state == 20) {
                intentionBeanList.add(list.get(i));
            }
        }
        adapter.notifyDataSetChanged();
    }

}
